package com.jingdianjichi.subject.domain.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.jingdianjichi.subject.domain.convert.SubjectCategoryConverter;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Service
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private ThreadPoolExecutor labelThreadPool;
    /**
     * 添加分类
     * @param subjectCategoryBO
     */
    @Override
    public void add(SubjectCategoryBO subjectCategoryBO){
        SubjectCategory subjectCategory = SubjectCategoryConverter
                .INSTANCE.convertBOToCategory(subjectCategoryBO);
        subjectCategoryService.insert(subjectCategory);
    }

    /**
     * 查询分类列表
     * @return
     */
    @Override
    public List<SubjectCategoryBO> queryCategoryList(SubjectCategoryBO subjectCategoryBO) {
        List<SubjectCategory> subjectCategoryList =
                subjectCategoryService.queryCategoryList(SubjectCategoryConverter.INSTANCE.convertBOToCategory(subjectCategoryBO));
        List<SubjectCategoryBO> subjectCategoryBOList =SubjectCategoryConverter.INSTANCE.convertCategoryToBO(subjectCategoryList);
        if(subjectCategoryBOList == null || subjectCategoryBOList.isEmpty()){
            return Collections.emptyList();
        }
        subjectCategoryBOList.forEach(bo->{
            Integer count = subjectCategoryService.queryCount(bo.getId());
            bo.setCount( count);
        });
        return subjectCategoryBOList;
    }

    @Override
    public void deleteCategory(Long id) {
        subjectCategoryService.removeByCategoryId(id);
    }

    @Override
    public void update(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.convertBOToCategory(subjectCategoryBO);
        subjectCategoryService.updateInfo(subjectCategory);
    }

    @SneakyThrows
    @Override
    public List<SubjectCategoryBO> queryCategoryListAndLabel(SubjectCategoryBO subjectCategoryBO) {
        //查询大类下所有的分类
        SubjectCategory subjectCategory = new SubjectCategory();
        subjectCategory.setId(subjectCategoryBO.getId());
        subjectCategory.setParentId(subjectCategoryBO.getId());
        subjectCategory.setIsDeleted(0);
        List<SubjectCategory> subjectCategories = subjectCategoryService.queryCategoryList(subjectCategory);

        //转换为bo
        List<SubjectCategoryBO> subjectCategoryBOList = SubjectCategoryConverter.INSTANCE.convertCategoryToBO(subjectCategories);
        //遍历添加标签
        List<CompletableFuture<Map<Long, List<SubjectLabelBO>>>> completableFutures = subjectCategoryBOList.stream().map(category ->
                CompletableFuture.supplyAsync(() -> getSubjectLabelBOS(category), labelThreadPool)
        ).collect(Collectors.toList());

        Map<Long,List<SubjectLabelBO>> map = new HashMap<>();

        for (CompletableFuture<Map<Long, List<SubjectLabelBO>>> completableFuture : completableFutures) {
            Map<Long, List<SubjectLabelBO>> rMap = completableFuture.get();
            if(CollectionUtils.isEmpty(rMap)){
                continue;
            }
            map.putAll(rMap);
        }
        subjectCategoryBOList.forEach(bo->{
            bo.setSubjectLabelBOList(map.get(bo.getId()));
        });



/*        subjectCategoryBOList.forEach(bo->{
            List<SubjectLabelBO> subjectLabelBOList = getSubjectLabelBOS(bo);
            if (subjectLabelBOList == null) return;
            bo.setSubjectLabelBOList(subjectLabelBOList);
        });*/

        return subjectCategoryBOList;
    }

    private Map<Long,List<SubjectLabelBO>> getSubjectLabelBOS(SubjectCategoryBO bo) {
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(bo.getId());
        subjectMapping.setIsDeleted(0);
        //查询到所有的标签id
        List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelIdsByCategory(subjectMapping);
        if(CollectionUtils.isEmpty(subjectMappingList)){
            return null;
        }
        List<Long> labelIds = subjectMappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());

        //根据id集合查询标签
        List<SubjectLabel> subjectLabels = subjectLabelService.batchQueryByLabelIds(labelIds);
        List<SubjectLabelBO> subjectLabelBOList = new ArrayList<>();

        Map<Long,List<SubjectLabelBO>> map = new HashMap<>();
        subjectLabels.forEach(label->{
            SubjectLabelBO subjectLabelBO = new SubjectLabelBO();
            subjectLabelBO.setId(label.getId());
            subjectLabelBO.setLabelName(label.getLabelName());
            subjectLabelBO.setSortNum(label.getSortNum());
            subjectLabelBOList.add(subjectLabelBO);
        });
        map.put(bo.getId(),subjectLabelBOList);
        return map;
    }

}
